<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 * 用户model
 * @Author Abell
 * @DateTime 2020-04-16
 */
class Wxuser extends Model
{
    /**
     * The table associated with the model.
     * @var string
     */
    protected $table = 'wxuser';
    /**
     * The attributes that are mass assignable.
     * @var array
     */
    protected $fillable = [];

    const CREATED_AT = 'add_time';

    const UPDATED_AT = 'update_time';

    protected $casts = [
        'add_time'=>'datetime:Y-m-d H:i:s',
        'update_time'=>'datetime:Y-m-d H:i:s',
    ];

    /**
     * @param [string] $openid
     * @return array
     * @Description  获取rawData更新用户信息
     * @author abell
     * @since 2020-05-26
     */
    public function update_user($data){

    }

    /**
     * 获取系统用户列表
     * 可以传入条件
     * User：Abell
     * Date：2020/4/16
     * Time：17:28
     * @param array $where 查询条件
     * @param array $order 排序条件
     * @param int $offset 偏移
     * @param int $limit 条数
     * @return array
     */
    public function getUserList($where = [], $order = [], $offset = 0, $limit = 0){
        $query = $this->query()->select($this->table . '.id', $this->table . '.openid', $this->table . '.fid', $this->table . '.nickname', $this->table . '.avatarurl', $this->table . '.gender', $this->table . '.country', $this->table . '.country', $this->table . '.province', $this->table . '.city', $this->table . '.language', $this->table . '.ctime', $this->table . '.mobile', $this->table . '.last_login', $this->table . '.haspay');
        // 循环增加查询条件
        foreach ($where as $k => $v) {
            if ($v || $v != null) {
                $query = $query->where($this->table . '.' . $k, $v);
            }
        }
        // 追加排序
        if ($order && is_array($order)) {
            foreach ($order as $k => $v) {
                $query = $query->orderBy($this->table . '.' . $k, $v);
            }
        }
        // 是否分页
        if ($limit) {
            $query = $query->offset($offset)->limit($limit);
        }
        $query = $query->get();
        return $query ? $query->toArray() : [];
    }


}